package persistencia;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;


/**
 * Conectar e fazer requisicoes ao mysql
 * @author Pablo Soares
 *
 */
public class BancoDeDados {
	
	private static String driverJDBC = "com.mysql.jdbc.Driver";
	private static String domain = "jdbc:mysql://localhost/RPTICKET";
	private static String user = "root";
	private static String pass = "";
	private static Connection con;
	private static Statement stm;
        public ResultSet resultset;
        
	public static void conecta(){
		try {
			Class.forName(driverJDBC);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			con = DriverManager.getConnection(domain, user, pass);
                        stm = con.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void desconectar(){
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static boolean queryUpdate(String query){		
		boolean r = false;
		try {
			System.out.println(query);
			r = stm.execute(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return r;
	}
	
	public static ResultSet query(String query){
		ResultSet res = null;
		try {
			System.out.println(query);
			res = stm.executeQuery(query);			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}

	
	public static void setParametros(String driver, String url, String login, String senha) {
		driverJDBC = driver;
		domain = url;
		user = login;
		pass = senha;		
	}
		
	public static String getDriverJDBC() {
		return driverJDBC;
	}

	public static void setDriverJDBC(String driverJDBC) {
		BancoDeDados.driverJDBC = driverJDBC;
	}

	public static String getUrl() {
		return domain;
	}

	public static void setUrl(String url) {
		BancoDeDados.domain = url;
	}

	public static String getLogin() {
		return user;
	}

	public static void setLogin(String login) {
		BancoDeDados.user = login;
	}

	public static String getSenha() {
		return pass;
	}

	public static void setSenha(String senha) {
		BancoDeDados.pass = senha;
	}

	public static Connection getConexao() {
		return con;
	}

	public static void setCon(Connection con) {
		BancoDeDados.con = con;
	}

	public static Statement getStm() {
		return stm;
	}

	public static void setStm(Statement stm) {
		BancoDeDados.stm = stm;
	}
}